goog.provide('dkpoints.rpc.commands.Command');
goog.provide('dkpoints.rpc.commands.CommandResponse');



/**
 * The abstract base class for all RPC Command objects.
 * @param {function()} onSuccess Called on a successful response.
 * @param {function(goog.net.XhrManager.Event)} onFailure Called if the
 *    request fails.
 * @param {string} commandID The ID for this command.
 * @param {string} url The URL to request.
 * @param {string=} method GET or POST (defaults to POST).
 * @param {number=} maxRetries The number of retries.
 * @constructor
 */
dkpoints.rpc.commands.Command = function(onSuccess, onFailure, commandID,
                                         url, method, maxRetries) {
  /**
   * Get or Post request.  Default to Post.
   * @type {string}
   */
  this.method = method || 'POST';

  /**
   * Occasionally, even a cacheable command will need to be fetched instead of
   * read from the cache.  Only subclasses that are cacheable will ever have to
   * override this flag.
   * @type {boolean}
   */
  this.force = false;

  /**
   * Defaults to not caching the response.  Subclass can override after calling
   * this constructor.
   * @type {boolean}
   */
  this.shouldCache = false;

  /**
   *  Defaults to not batching requests of this same type.  Subclass can
   *  override after calling this constructor.
   *  @type {boolean}
   */
  this.shouldBatch = false;

  /**
   * The callback to the originating code for a successful request.
   * @type {function(dkpoints.rpc.commands.CommandResponse)}
   * @private
   */
  this.callersOnSuccess_ = onSuccess;

  /**
   * The callback to the originating code for a failed request.  The event is
   * simply passed back to the caller to pick out specific details needed to
   * diagnose the problem.
   * @type {function(goog.net.XhrManager.Event)}
   * @private
   */
  this.callersOnFailure_ = onFailure;

  /**
   * The identifier for this Command.  May or may not be unique, depending on
   * the subclass.
   * @type {string}
   * @private
   */
  this.commandID_ = commandID;

  /**
   * The URL to request.
   * @type {string}
   */
  this.url = url;

  /**
   * The number of retries before admitting failure.
   * 'undefined' will cause XhrManager to use default.
   * @type {number|undefined}
   */
  this.maxRetries = maxRetries;

  /**
   * At the very least, the commandID is sent to the server as POST data in
   * order to match responses with requests.  The subclass can add new items
   * to this if necessary after calling this constructor.
   * @type {!Object}
   */
  this.data = {'commandID': commandID};
};


/**
 * Get the CommandID for this command object.
 * @return {string} The commandID value for this command.
 */
dkpoints.rpc.commands.Command.prototype.getCommandID = function() {
  return this.commandID_;
};


/**
 * This method is called by the RPCService on a successful response.
 * Each one will need to know how to process the specific XHR returned data into
 * the appropriate CommandResponse object.
 *
 * All Command subclasses must override this.
 *
 * @param {goog.net.XhrManager.Event} event The COMPLETE event for the XHR Req.
 */
dkpoints.rpc.commands.Command.prototype.onSuccess = goog.abstractMethod;


/**
 * This method is called by the RPCService on a failed response.
 * All Command subclasses must override this.
 *
 * @param {goog.net.XhrManager.Event} event The COMPLETE event for the XHR Req.
 */
dkpoints.rpc.commands.Command.prototype.onFailure = goog.abstractMethod;



/**
 * The abstract parent class of all Command responses.
 * @param {Object} serverResponse The parsed JSON object returned by the server.
 * @constructor
 */
dkpoints.rpc.commands.CommandResponse = function(serverResponse) {
  /**
   * @type {Object}
   * @private
   */
  this.response_ = serverResponse;
};
